package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.DestinationThemeQuery;
import cn.wolfcode.luowowo.article.query.RegionQuery;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {
    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService catalogService;
    @Reference
    private IStrategyDetailService detailService;
    @Reference
    private ITravelService travelService;
    //目的地推荐服务
    @Reference
    private IDestinationThemeService destinationThemeService;
    @Reference
    private IDestinationThemeTagService destinationThemeTagService;
    @Reference
    private IDestinationCommentService destinationCommentService;
    @Reference
    private IDestinationThemeRegionService destinationThemeRegionService;

    @RequestMapping("")
    public String index(Model model) {
        RegionQuery qo = new RegionQuery();
        qo.setIshot(1);
        List<Region> hotRegions = regionService.selectForList(qo);
        model.addAttribute("hotRegions", hotRegions);
        //destThemes 所有的目的地推荐
        List<DestinationTheme> destThemes = destinationThemeService.list();
        model.addAttribute("destThemes", destThemes);
        //所有的月份数据
        List<DestinationThemeRegion> destRegions = destinationThemeRegionService.list();
        model.addAttribute("destRegions", destRegions);
        return "destination/index";
    }
    @RequestMapping("getHotDestByRegionId")
    public String getHotDestByRegionId(Long regionId, Model model) {
        List<Destination> hotDests = regionService.getHotDestByRegionId(regionId);
        List<Destination> rightDests = new ArrayList<>();
        List<Destination> leftDests = new ArrayList<>();
        for (int i = 0; i < hotDests.size(); i++) {
            if (i % 2 != 0) {
                rightDests.add(hotDests.get(i));
            } else {
                leftDests.add(hotDests.get(i));
            }
        }
        model.addAttribute("leftDests", leftDests);
        model.addAttribute("rightDests", rightDests);
        model.addAttribute("regionId", regionId);
        return "destination/hotdestTpl";
    }
    @RequestMapping("guide")
    public String guide(Long id, Model model) {
        Destination destination = destinationService.getById(id);
        List<Destination> toasts = destinationService.getToasts(id);
        toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", destination);
        //catalogs
        List<StrategyCatalog> catalogs = catalogService.getCatalogByDestId(id);
        model.addAttribute("catalogs", catalogs);
        //strategyDetails
        List<StrategyDetail> strategyDetails = detailService.getTopViewnum3ByDestId(id);
        model.addAttribute("strategyDetails", strategyDetails);
        return "destination/guide";
    }

    @RequestMapping("surveyPage")
    public String surveyPage(@ModelAttribute("qo") StrategyDetailQuery qo, Model model) {
        //dest
        Destination dest = destinationService.getById(qo.getDestId());
        model.addAttribute("dest", dest);
        //toasts
        List<Destination> toasts = destinationService.getToasts(dest.getParent().getId());
        model.addAttribute("toasts", toasts);
        return "destination/survey";
    }
    @RequestMapping("survey")
    public String survey(@ModelAttribute("qo") StrategyDetailQuery qo, Model model) {
        //catalog
        StrategyCatalog catalog = catalogService.get(qo.getCatalogId());
        model.addAttribute("catalog", catalog);
        //catalogs
        List<StrategyCatalog> catalogs = catalogService.getCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs", catalogs);
        StrategyDetail detail = detailService.get(catalog.getDetails().get(0).getId());
        model.addAttribute("detail", detail);
        return "destination/surveyTpl";
    }
    @RequestMapping("travels")
    public String travels(@ModelAttribute("qo") TravelQuery qo, Model model) {
        model.addAttribute("pageInfo", travelService.query(qo));
        return "destination/travelTpl";
    }
    /**
     * 主题精选
     *
     * @param model   共享
     * @param themeId 主题id
     * @return 目的地集合
     */
    @RequestMapping("getTagByThemeId")
    public String getTagByThemeId(Model model, Long themeId) {

        List<DestinationThemeTag> tags = destinationThemeTagService.queryTagByThemeId(themeId);
        model.addAttribute("tags", tags);

        return "destination/destThemeTpl";
    }

    //主题条件过滤搜索
    @RequestMapping("destFilter")
    public String destFilter(Model model, Long tagId, @ModelAttribute("qo") DestinationThemeQuery qo) {
        //根据页面传递过来的标签id查询推荐数据
        DestinationThemeTag destinationThemeTag = destinationThemeTagService.get(tagId);
        //所有的月份数据
        List<DestinationThemeRegion> destRegions = destinationThemeRegionService.list();
        //  查询主题推荐分类 4个
        List<DestinationTheme> themes = destinationThemeService.list();
        List<DestinationThemeTag> tags = new ArrayList<>();

        for (DestinationTheme theme : themes) {
            //根据分类查询分类数据
            tags = destinationThemeTagService.queryTagByThemeId(theme.getId());
            theme.setTags(tags);
        }
        model.addAttribute("destinationThemeTag", destinationThemeTag);
        model.addAttribute("themes", themes);
        model.addAttribute("tags", tags);
        model.addAttribute("destRegions", destRegions);
        model.addAttribute("tagId",tagId);
        return "destination/destFilter";
    }
    //目的地省份和TOP3测试的模板
    @RequestMapping("getCitys")
    public String getDestPage(Model model) {

       // children:用来接收子地区的数组
        //城市级别
        List<Destination> citys =new ArrayList<>();
                //查询省份城市
        List<Destination> list = destinationService.getDestByDeep(1);
        //随机截取前6个省份
        List<Destination> dests = destinationService.getRandomList(list, 6);
        for (Destination dest : dests) {
            //1个省份对应多个城市
           citys = destinationService.getCitys(dest.getId());
           //查询3个城市,否则会出现数组索引越界的问题
           List<Destination> randomList = destinationService.getRandomList(citys, 3);
           //为每一个省份设置城市数据
            dest.setChildren(randomList);
        }
        model.addAttribute("dests",dests);//前6个城市
        return "destination/destCityTpl" ;
    }
    //月份查询目的地
    @RequestMapping("getDestByRegionId")
    public String getDestByRegionId(Long regionId, Model model) {
        List<Destination> dests = destinationThemeRegionService.getDestByRegionId(regionId);
        model.addAttribute("dests", dests);
        model.addAttribute("regionId", regionId);
        return "destination/destRegionTpl";
    }
}